package UCM;

import Source.LoginUser;
import Views.ViewLoginInt;

/**
 * The use case for login user
 * @author Jason and Joshua
 */
public class UCLoginUser implements UCController {

    private LoginUser model = new LoginUser();
    private ViewLoginInt view;

    public UCLoginUser(ViewLoginInt aView) {
        this.view = aView;
        view.loadTo(model);
    }


    /**
     * Precondition: User on Main UI.

    1. User enters their details and clicks login.
    2. System gets data entered by the user.
    3. System authenticates user.
    4. System adds user to list of online users.
    5. System updates UI indicating success.
    
    Postcondition: The main UI is updated.

    Unsuccessful Outcomes:

    2a) The authentication fails.
    System logs failed attempt.
    System refreshes UI indicating failure.
     */
    public void run() {
        
        //2. System gets data entered by the user.
        view.setModelData();
        //3. System authenticates user.
        if (!model.authenticate()) {
            //2a) The authentication fails.
            //System logs failed attempt.
            model.logFailedAttempt();
            //System refreshes UI indicating failure.
            view.updateFailure();
        } else {
            //4. System adds user to list of online users.
            model.addToOnline();
            //5. System updates UI indicating success.
            view.updateSuccess();            
        }

    }
}
